package auth

import (
	"github.com/gin-gonic/gin"
	v1 "last/app/controllers/v1"
	adminModel "last/app/models/admin"
	"last/app/requests"
	adminRequest "last/app/requests/admin"
	"last/pkg/respose"
	"strconv"
)

type FriendController struct {
	v1.BaseController
}

// Store 新增
func (f *FriendController) Store(c *gin.Context) {
	var params = adminRequest.FriendParams{}
	err := requests.Validator(&params, c)
	if err != nil {
		respose.Failed(c, err.Error())
		return
	}
	var friendInfo = &adminModel.Friends{
		SiteName: params.SiteName,
		Desc:     params.Desc,
		Path:     params.Path,
	}
	friendBool := friendInfo.Store(friendInfo)
	if !friendBool {
		respose.Failed(c, "新增失败")
	}
	respose.Success(c, "", "新增成功")
}

// List 列表
func (f *FriendController) List(c *gin.Context) {
	var friendInfo = adminModel.Friends{}
	where := make(map[string]interface{}, 1)
	where["site_name"] = c.DefaultQuery("site_name", "")
	page, _ := strconv.Atoi(c.Query("page"))
	pageSize, _ := strconv.Atoi(c.Query("page_size"))

	err, total, lists := friendInfo.List(page, pageSize, where)
	if err != nil {
		respose.Failed(c, "获取失败")
		return
	}
	result := make(map[string]interface{}, 1)
	result["total"] = total
	result["list"] = lists
	respose.Success(c, result, "获取成功")
}

// Show 详情
func (f *FriendController) Show(c *gin.Context) {
	id := c.Param("id")
	var friendInfo = &adminModel.Friends{}
	err := friendInfo.Show(id, friendInfo)
	if err != nil {
		respose.Failed(c, err.Error())
		return
	}
	respose.Success(c, friendInfo, "获取成功")
}

// Edit 更新
func (f *FriendController) Edit(c *gin.Context) {
	var params = adminRequest.FriendParams{}
	err := requests.Validator(&params, c)
	if err != nil {
		respose.Failed(c, err.Error())
		return
	}
	editData := make(map[string]interface{}, 1)
	editData["site_name"] = params.SiteName
	editData["desc"] = params.Desc
	editData["path"] = params.Path
	var friendInfo = adminModel.Friends{}
	result := friendInfo.Edit(params.Id, editData)
	if result != nil {
		respose.Failed(c, "更新失败")
		return
	}
	respose.Success(c, "", "更新成功")
}

// Remove 删除
func (f *FriendController) Remove(c *gin.Context) {
	id := c.Param("id")
	var friendInfo = adminModel.Friends{}
	err := friendInfo.Remove(id, friendInfo)
	if err != nil {
		respose.Failed(c, err.Error())
		return
	}
	respose.Success(c, "", "删除成功")
}
